#include"WorkThreads.h"
#include"./net/Server.h"
#include"./log/Log_Def.h"
#define PORT 5566

void NetThread::Run(){
   LogType log_type_;
  ostringstream oss;
	sleep(1);
	Server *s = new Server(AF_INET,PORT,INADDR_ANY,SOCK_STREAM,log_queue_);
  bool wrong = false;
  for(;;){
    try{
      s->Init();
    }catch(int a){
      wrong = true;
      cout << "Init" << a << endl;
      cout << string(strerror(errno)) << endl;
      if(a==-1){
        LOG_INFO(LOG_LEVEL_ERROR) << "NET LOG ** " <<"BIND FAILED AND reason is :"\
                                           <<  string(strerror(errno)); 
        LOG_ADD;
      }
      else if(a==-2){
        LOG_INFO(LOG_LEVEL_ERROR) << "NET LOG ** " <<"LISTEN FAILED AND reason is :"\
                                           <<  string(strerror(errno)); 
        LOG_ADD;
      }
    }
    if(wrong == false){

			cout<<"net log add success "<<endl;
      LOG_INFO(LOG_LEVEL_INFO) << "NET LOG ** " <<"NETWORK CONNECT SUCCESS"; 
      LOG_ADD;
    }
    wrong =false;
    try{
      s->Accept();
    }catch(int a){
      cout << "accept" << a <<endl;
      cout <<  string(strerror(errno)) << endl;
      if(a==0){
        LOG_INFO(LOG_LEVEL_ERROR) << "NET LOG ** " <<"SELLECT FAILED AND reason is :"\
                                           <<  string(strerror(errno)); 
        LOG_ADD;
      }
      else if(a==1){
        LOG_INFO(LOG_LEVEL_ERROR) << "NET LOG ** " <<"ACCEPT FAILED AND reason is :"\
                                           <<  string(strerror(errno)); 
        LOG_ADD;
      }
      else if(a==-1){
        LOG_INFO(LOG_LEVEL_ERROR) << "NET LOG ** " <<"Other Decode Error"; 
        LOG_ADD;
      }
      else if(a==-2){
        LOG_INFO(LOG_LEVEL_ERROR) << "NET LOG ** " <<"DATA SIZE or DATA NAME not correct"; 
        LOG_ADD;
      }
      else if(a==-3){
        LOG_INFO(LOG_LEVEL_ERROR) << "NET LOG ** " <<"DATA RECEIVE FAILED"; 
        LOG_ADD;
      }
      else if(a==-4){
        LOG_INFO(LOG_LEVEL_ERROR) << "NET LOG ** " <<"RECEIVE SET FAILED"; 
        LOG_ADD;
      }
      else if(a==-5){
        LOG_INFO(LOG_LEVEL_ERROR) << "NET LOG ** " <<"RECEIVE PRM_NUM FAILED"; 
        LOG_ADD;
      }
      else if(a==-6){
        LOG_INFO(LOG_LEVEL_ERROR) << "NET LOG ** " <<"RECEIVE TIME FAILED"; 
        LOG_ADD;
      }
      else if(a==-7){
        LOG_INFO(LOG_LEVEL_ERROR) << "NET LOG ** " <<"RECEIVE IP FAILED"; 
        LOG_ADD;
      }
    }

  }
}
